package com.roncoo.education.api;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.github.pagehelper.PageInfo;
import com.roncoo.education.api.request.NewsCommentRequest;
import com.roncoo.education.api.request.NewsRequest;
import com.roncoo.education.db.entity.News;
import com.roncoo.education.db.entity.NewsCategory;
import com.roncoo.education.db.entity.NewsComment;
import com.roncoo.education.db.service.MyHistoryService;
import com.roncoo.education.db.service.NewsCategoryService;
import com.roncoo.education.db.service.NewsCommentService;
import com.roncoo.education.db.service.NewsService;
import com.roncoo.education.util.MyJwtUtil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api")
@CrossOrigin(origins = "*", allowedHeaders = "*")
public class NewsController {

    @Resource
    private NewsService newsService;

    @Resource
    private NewsCommentService newsCommentService;

    @Resource
    private NewsCategoryService newsCategoryService;

    @Resource
    MyHistoryService myHistoryService;

    @Resource
    MyJwtUtil myJwtUtil;

    @GetMapping("/getNewsCategorys")
    public String getNewsCategorys(Integer isHot){
        NewsCategory query = new NewsCategory();
        query.setDelFlag(1);
        query.setIsHot(isHot);
        query.setOrderClause(" sn asc");
        List<NewsCategory> newsCategories = newsCategoryService.findByBean(query);
        return JSONUtil.toJsonStr(newsCategories);
    }

    @PostMapping("/getNewsPages")
    public String getNewsPages(@Valid @RequestBody NewsRequest req){
        PageInfo<News> pageInfo = newsService.getNewsPages(req);
        return JSONUtil.toJsonStr(pageInfo);
    }

    @PostMapping("/getLastNewsPages")
    public String getLastNewsPages(){
        NewsRequest req = new NewsRequest();
        req.setPageNum(1);
        req.setCount(10);
        req.setOrderClause(" a.id desc");
        PageInfo<News> pageInfo = newsService.getNewsPages(req);
        return JSONUtil.toJsonStr(pageInfo);
    }

    @GetMapping("/getNewsDetail")
    public String getNewsDetail(Long id,HttpServletRequest request){
        News news = newsService.getNewById(id);
        if(news == null){
            return "error";
        }
        newsService.newsViewsNumAdd(news.getId(),news.getViews());
        String token = request.getHeader("token");
        if(StrUtil.isNotBlank(token)) {
            Long userId = myJwtUtil.getUsernameFromToken(token);
            myHistoryService.saveHistory(userId, id, 1);
        }
        return JSONUtil.toJsonStr(news);
    }

    @PostMapping("/saveNewsComment")
    public String saveNewsComment(@Valid @RequestBody NewsCommentRequest req, HttpServletRequest request){
        String token = request.getHeader("token");
        Long userId = myJwtUtil.getUsernameFromToken(token);
        req.setUserId(userId);
        Map<String,String> ret = newsCommentService.saveNewsComment(req);
        return JSONUtil.toJsonStr(ret);
    }

    @PostMapping("/getNewsCommentPages")
    public String getNewsComments(@RequestBody NewsRequest req){

        PageInfo<NewsComment> pageInfo = newsCommentService.getCommentIndexPages(req);
        return JSONUtil.toJsonStr(pageInfo);
    }
}
